package com.MusicWeb.service;

import com.MusicWeb.dao.DB;
import com.MusicWeb.entity.Column;
import com.MusicWeb.entity.Tip;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class TipService {
    /*
    发送信息
     */
    public int TipAdd(Tip tip) {
        try {
            int res = DB.executeUpdate("INSERT INTO `tip`( `title`, `text`, `user_id`, `music_id`,`from_id`)" +
                            " VALUES ( ? , ? , ? , ? , ? )"
                    , tip.getTitle()
                    , tip.getText()
                    , tip.getUid()
                    , tip.getMid()
                    , tip.getFrom_id()
            );
            if (res != -1) {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
        return -1;
    }

    /*
    删除信息
     */
    public int Tipdel(Integer id) {
        try {
            int res = DB.executeUpdate("delete from tip where id = ? ", id);
            if (res != -1) {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
        return -1;
    }

    /*
    查询信息
     */

    public List<Tip> TipSelect(Integer uid) {
        List<Map<String, Object>> res = DB.executeQuery("select t.* , u.name from tip t  join user u on t.from_id=u.id where u.id=", uid);
        List<Tip> list = new ArrayList<>();
        if (res != null) {
            for (Map<String, Object> stringObjectMap : res) {
                Tip tip = new Tip();
                tip.setId((Integer) stringObjectMap.get("id"));
                tip.setTitle((String) stringObjectMap.get("title"));
                tip.setText((String)stringObjectMap.get("text"));
                tip.setUid((Integer) stringObjectMap.get("user_id"));
                tip.setMid((Integer) stringObjectMap.get("music_id"));
                tip.setDate((String.valueOf(stringObjectMap.get("create_time"))));
                tip.setName((String)stringObjectMap.get("name"));
                list.add(tip);
            }
        }
        return list;

    }

    public  List<Tip> TipSe(Integer uid){
        List<Tip> list=DB.runSql("select t.* , u.name from tip t  join user u on t.from_id=u.id where t.user_id = ? ",uid);
        return list;
    }

    public static String reformatDate(String date) {
        String Month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        String[] str = date.split(" ");
        String[] res = new String[3];
        for (int i = 0; i < str.length; i++) {
            if (str[i].length() == 4&&str[i].charAt(3)<='9') {
                res[0]=str[i];
                //年份操作
            } else if ('9'<str[i].charAt(0)) {
                //月份操作
                for (int j = 0; j < Month.length; j++) {
                    if (str[i].equals(Month[j])) {
                        if ((j + 1) < 10) {
                            res[1] = "-0" + (j + 1) + "";
                        } else {
                            res[1] ="-"+(j + 1);
                        }
                    }
                }
            } else if ('9'>=str[i].charAt(0)&&str[i].charAt(2)>='9'){
                //日操作
                if (str[i].charAt(1)<='9'){
                    res[2]="-"+str[i].substring(0,2);
                }else {
                    res[2] = "-0" + str[i].charAt(0);
                }
            }
        }
        return  res[0]+res[1]+res[2];
    }
}
